package com.example.taxfloatproject20240926.ui;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.taxfloatproject20240926.R;
import com.example.taxfloatproject20240926.ui.utils.Constant;
import com.example.taxfloatproject20240926.ui.utils.EncryptionAlgorithm;
import com.example.taxfloatproject20240926.ui.utils.PreferencesUtils;


/**
 * Created by gf on 15-1-22.
 */
public class RegisterActivity extends Activity {
    private TextView m_code;
    private EditText et_reg_content;
    private Button btn_reg_sure, btn_reg_cancel;
    private Context context;
    public static final String TAG = "RegisterActivity";

    String seriaNumber;
    private boolean isPermission;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_register);
        context = this;
        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(this)) {
                //若未授权则请求权限
                getOverlayPermission();
            }else {
                isPermission = true;
            }
        }else {
            isPermission = true;
        }
        initView();
    }


    private void initView() {
        m_code = findViewById(R.id.m_code);
        String strMathineCode = Settings.System.getString(this.getContentResolver(), "android_id").substring(3, 9);
        et_reg_content = findViewById(R.id.et_reg_content);
        btn_reg_sure = findViewById(R.id.btn_reg_sure);
        btn_reg_cancel = findViewById(R.id.btn_reg_cancel);
        m_code.setText(strMathineCode);
        EncryptionAlgorithm localEncryptionAlgorithm = new EncryptionAlgorithm();

        strMathineCode += "yida1234";
        Log.i(TAG, "str___===" + strMathineCode);
        seriaNumber = localEncryptionAlgorithm.MD5(strMathineCode);

//        Intent intent = new Intent(context,MainActivity.class);
//        startActivity(intent);
//        finish();

        String registerCode = PreferencesUtils.getString(context, Constant.REGESTER_KEY);
        if (!TextUtils.isEmpty(registerCode) && registerCode.length() > 0 && isPermission) {
            Intent intent = new Intent(context, MainActivity.class);
            startActivity(intent);
            finish();
        }

        btn_reg_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String registerCode = et_reg_content.getText().toString();
                if (registerCode.equals(seriaNumber) && isPermission) {
                    PreferencesUtils.putString(context, Constant.REGESTER_KEY, registerCode);
                    startActivity(new Intent(context, MainActivity.class));
                    finish();
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                } else {
                    PreferencesUtils.putString(context, Constant.REGESTER_KEY, "");
                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_LONG).show();
                }
            }
        });

        btn_reg_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //请求悬浮窗权限
    @TargetApi(Build.VERSION_CODES.M)
    private void getOverlayPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, 0);
    }


    private void disableKeyguard() {
        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("");
        keyguardLock.disableKeyguard();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        isPermission = true;
    }
}
